(function ($) {

  /**
   * Chain select lists.
   */
  Drupal.behaviors.chainedSelects = {
    attach: function (context) {

      if (Drupal.settings.chainedSelects_backup == undefined) {
        Drupal.settings.chainedSelects_backup = {};
      }

      var chainedSelects = Drupal.settings.chainedSelects;
      for (var key in chainedSelects) {
        if (Drupal.settings.chainedSelects_backup[key] == undefined) {
          Drupal.settings.chainedSelects_backup[key] = {};
          Drupal.settings.chainedSelects_backup[key]['html'] = $('#' + chainedSelects[key]['child'] + ':input').html();
        }
        var val = $('#' + chainedSelects[key]['child'] + ':input').val();
        $('#' + chainedSelects[key]['child'] + ':input').html(Drupal.settings.chainedSelects_backup[key]['html']);
        $('#' + chainedSelects[key]['child'] + ':input').chained('#' + chainedSelects[key]['parent'] + ':input');
        $('#' + chainedSelects[key]['child'] + ':input').val(val);
      }
    }
  };

})(jQuery);
